/**
 * localStorage 使用示例
 * 演示如何使用 LocalStorage 类和 localStorageKeys
 */

import LocalStorage from './localStorage';
import localStorageKeys from './localStorageKeys';

// ============================================
// 基本用法示例
// ============================================

// 1. 存储简单数据
LocalStorage.setItem(localStorageKeys.USER_THEME, 'dark');
LocalStorage.setItem(localStorageKeys.CURRENT_WEEK, 5);

// 2. 存储对象数据
LocalStorage.setItem(localStorageKeys.SELECTED_SCHOOL, {
  id: 1,
  name: '福州大学',
});

// 3. 存储数组数据
LocalStorage.setItem(localStorageKeys.CACHE_SCHOOLS, [
  { id: 1, name: '福州大学' },
  { id: 2, name: '厦门大学' },
]);

// 4. 获取数据（带默认值）
const theme = LocalStorage.getItem(localStorageKeys.USER_THEME, 'light');
const week = LocalStorage.getItem(localStorageKeys.CURRENT_WEEK, 1);
const school = LocalStorage.getItem(localStorageKeys.SELECTED_SCHOOL, null);

// 5. 删除数据
LocalStorage.removeItem(localStorageKeys.USER_THEME);

// 6. 清空所有数据
LocalStorage.clear();

// ============================================
// 实际应用场景示例
// ============================================

// 场景1：保存用户选择的周次
export const saveCurrentWeek = (week) => {
  LocalStorage.setItem(localStorageKeys.CURRENT_WEEK, week);
};

export const getCurrentWeek = () => LocalStorage.getItem(localStorageKeys.CURRENT_WEEK, 1);

// 场景2：保存筛选条件
export const saveFilters = (filters) => {
  LocalStorage.setItem(localStorageKeys.FILTER_ACADEMIC_YEAR, filters.academicYear);
  LocalStorage.setItem(localStorageKeys.FILTER_TERM, filters.term);
  LocalStorage.setItem(localStorageKeys.FILTER_CAMPUS, filters.campus);
  LocalStorage.setItem(localStorageKeys.FILTER_COLLEGE, filters.college);
  LocalStorage.setItem(localStorageKeys.FILTER_GRADE, filters.grade);
  LocalStorage.setItem(localStorageKeys.FILTER_MAJOR, filters.major);
  LocalStorage.setItem(localStorageKeys.FILTER_EDUCATION_LEVEL, filters.educationLevel);
};

export const getFilters = () => ({
  academicYear: LocalStorage.getItem(localStorageKeys.FILTER_ACADEMIC_YEAR),
  term: LocalStorage.getItem(localStorageKeys.FILTER_TERM),
  campus: LocalStorage.getItem(localStorageKeys.FILTER_CAMPUS),
  college: LocalStorage.getItem(localStorageKeys.FILTER_COLLEGE),
  grade: LocalStorage.getItem(localStorageKeys.FILTER_GRADE),
  major: LocalStorage.getItem(localStorageKeys.FILTER_MAJOR),
  educationLevel: LocalStorage.getItem(localStorageKeys.FILTER_EDUCATION_LEVEL),
});

// 场景3：缓存API数据（带过期时间）
export const setCacheWithExpiry = (key, data, expiryMinutes = 5) => {
  const item = {
    data,
    expiry: Date.now() + expiryMinutes * 60 * 1000,
  };
  LocalStorage.setItem(key, item);
};

export const getCacheWithExpiry = (key) => {
  const item = LocalStorage.getItem(key);
  if (!item) return null;
  
  if (Date.now() > item.expiry) {
    LocalStorage.removeItem(key);
    return null;
  }
  
  return item.data;
};

// 使用示例
setCacheWithExpiry(localStorageKeys.CACHE_SCHOOLS, [{ id: 1, name: '福州大学' }], 60);
const cachedSchools = getCacheWithExpiry(localStorageKeys.CACHE_SCHOOLS);

// 场景4：保存最后访问时间
export const updateLastVisitTime = () => {
  LocalStorage.setItem(localStorageKeys.LAST_VISIT_TIME, new Date().toISOString());
};

export const getLastVisitTime = () => LocalStorage.getItem(localStorageKeys.LAST_VISIT_TIME);

// 场景5：在 React 组件中使用
/*
import { useEffect, useState } from 'react';
import LocalStorage from '@/utils/localStorage';
import localStorageKeys from '@/utils/localStorageKeys';

function CourseSchedule() {
  const [currentWeek, setCurrentWeek] = useState(() => 
    LocalStorage.getItem(localStorageKeys.CURRENT_WEEK, 1)
  );

  useEffect(() => {
    LocalStorage.setItem(localStorageKeys.CURRENT_WEEK, currentWeek);
  }, [currentWeek]);

  return (
    <div>
      <button onClick={() => setCurrentWeek(currentWeek + 1)}>
        下一周
      </button>
    </div>
  );
}
*/
